package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.pojo.*;
import com.dongdong.service.ItemService;
import com.dongdong.service.TbGoodsService;
import com.dongdong.service.TbSpecificationOptionService;
import com.dongdong.service.TypeTemplateService;
import com.dongdong.vo.SpecVO;
import com.dongdong.vo.TbTypeTemplateVo;
import com.dongdongshop.configresult.Result;
import com.dongdongshop.em.ResultEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("itemcontroller")
public class TbItemController {

    @Reference
    private ItemService itemService;

    @Reference
    private TypeTemplateService typeTemplateService;

    @Reference
    private TbSpecificationOptionService tbSpecificationOptionService;

    @Reference
    private TbGoodsService tbGoodsService;

    @RequestMapping("child")
    @ResponseBody
    public Result child(Long id){
        List<TbItemCat> itemCatList = itemService.selectChlid(id);
        if(itemCatList == null || itemCatList.size() <= 0){
            return Result.builResult(ResultEnum.ERROR);
        }
        return Result.builResult(ResultEnum.SUCCESS).setResout(itemCatList);
    }

    @RequestMapping("grandchild")
    @ResponseBody
    public Result grandchild(Long id){
        List<TbItemCat> itemCatList = itemService.selectChlid(id);
        if(itemCatList == null || itemCatList.size() <= 0){
            return Result.builResult(ResultEnum.ERROR);
        }
        return Result.builResult(ResultEnum.SUCCESS).setResout(itemCatList);
    }

    @RequestMapping("mukuai")
    @ResponseBody
    public Result mukuai(Long id){

//        查询type  ID
        TbItemCat itemCat = itemService.selectid(id);
        Long typeId = itemCat.getTypeId();

        TbTypeTemplate tbTypeTemplate = typeTemplateService.selectTypeId(typeId);
        if(tbTypeTemplate != null){
            return Result.builResult(ResultEnum.SUCCESS).setResout(tbTypeTemplate);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

    @RequestMapping("pinpai")
    @ResponseBody
    public Result pinpai(Long id){
        TbTypeTemplate tbTypeTemplate = typeTemplateService.selectId(id);
        if(tbTypeTemplate != null){
            return Result.builResult(ResultEnum.SUCCESS).setResout(tbTypeTemplate);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

    @RequestMapping("inserts")
    @ResponseBody
    public Result inserts(TbGoods tbGoods,TbGoodsDesc tbGoodsDesc,TbItem tbItem){
        Subject subject = SecurityUtils.getSubject();
        TbSeller tbSeller = (TbSeller)subject.getPrincipal();
        tbGoods.setSellerId(tbSeller.getSellerId());
        boolean b = tbGoodsService.insert(tbGoods,tbGoodsDesc,tbItem);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

    @RequestMapping("kuozhan")
    @ResponseBody
    public Result kuozhan(Long id){
        TbTypeTemplate tbTypeTemplate = typeTemplateService.selectId(id);
        if(tbTypeTemplate != null){
            return Result.builResult(ResultEnum.SUCCESS).setResout(tbTypeTemplate.getCustomAttributeItems());
        }
        return Result.builResult(ResultEnum.ERROR);
    }

//  规格
    @RequestMapping("guige")
    @ResponseBody
    public Result guige(Long id){
//        查询的模板

        TbTypeTemplate tbTypeTemplate = typeTemplateService.selectId(id);

//        创建对象   赋值
        TbTypeTemplateVo vo = new TbTypeTemplateVo();
        vo.setId(tbTypeTemplate.getId());
        vo.setName(tbTypeTemplate.getName());
        vo.setBrandIds(tbTypeTemplate.getBrandIds());
        vo.setCustomAttributeItems(tbTypeTemplate.getCustomAttributeItems());

//        取出模板中规格数据
        String specIds = tbTypeTemplate.getSpecIds();
//        转成对象
        List<SpecVO> specVOS = JSONObject.parseArray(specIds, SpecVO.class);
        for (SpecVO specVO : specVOS) {
            List<TbSpecificationOption> optionList = tbSpecificationOptionService.selectOptionsBySpecId(specVO.getId());
            specVO.setOptions(optionList);
        }

        vo.setSpecVOList(specVOS);
//        要返回品牌  扩展属性  规格
        return Result.builResult(ResultEnum.SUCCESS).setResout(vo);
    }


}
